<?php
namespace app\agent\controller;
use think\Controller;
use think\Db;
use think\request;
use app\common\controller\AgentBaseController;

class Activet extends AgentBaseController
{
	//鼓励金营销
	public function integral_marketing()
    {
    	$agentid = session('agent_id');
    	$list = Db::table('info_agent')->where('id',$agentid)->find();
    	$list['score'] 	 = substr($list['score'],0,strlen($list['score'])-2);
        $this->assign('list',$list);
        
        if($_POST){
        	$data['ad_score']=$_POST['ad_score'];
            $data['zs_score']=$_POST['zs_score'];
            $result=Db::table('info_agent')->where('id',$agentid)->update($data);;
            if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
    	return $this->fetch();
    }
    public function integral_marketing_up()
    {
        $agentid = session('agent_id');
    	$data['is_score']=input('scoreid');
        $result=Db::table('info_agent')->where('id',$agentid)->update($data);
        if($result){
			return json(['code'=>200,'msg'=>'成功']);
        }else{
       		return json(['code'=>402,'msg'=>'失败']);
        }
    }
    //联盟商圈已加入
	public function union_circle1()
    {
    	$agent_id = session('agent_id');
    	$agent_info = db('info_agent')->where('id',$agent_id)->find();
    	$agent_info['ad_money'] 	 = substr($agent_info['ad_money'],0,strlen($agent_info['ad_money'])-2);
    	$this->assign('agent_info',$agent_info);
    	
    	$where['b.state'] = 2;
    	$where['b.is_state'] = 1;
    	$where['a.agent_id'] = $agent_info['id'];
    	 
    	$list = db('union_member')
				->alias('a')
				->field('a.union_id,a.lm_name,a.add_time,a.customer_volume,a.state,b.id,b.add_time,b.sq_name,b.tel')
				->join('union_agent b','a.union_id = b.id')
				->where($where)
				->order('b.add_time desc')
				->paginate(20);
		
        $this->assign('list',$list);
    	$this->assign('empty','<span class="empty">没有数据</span>');
		
    	return $this->fetch();
    }
    
    //联盟商圈未加入,需要优化
    public function union_circle2()
    {
	  if(request()->isPost()){
    		$param=input('post.');
			if(!empty($param['name']))$where['name'] = ['like','%'.$param['store_name'].'%'];
    	}
    	
    	$where['state'] = 2;
    	$where['is_state'] = 1;
    	$list = Db::table('union_agent')
    			->where('id','not in',function($query){
    				$union_agent_where['agent_id'] 	= session('agent_id');
    				$union_agent_where['state'] 	= ['in','1,2'];
		    		$query->table('union_member')->where($union_agent_where)->field('union_id');
				})
				->where($where)
		    	->order('add_time desc')
		    	->paginate(20);
		    	
        $this->assign('list',$list);
        $this->assign('empty','<span class="empty">没有数据</span>');
		
    	return $this->fetch();
    }
    //联盟加入成员
	public function union_join()
    {
    	if(request()->isPost()){
    		$union=Db::table('union_agent')->where('id',input('lid'))->find();
    		$agent=Db::table('info_agent')->where('id',session('agent_id'))->find();
    		$param['u_id']				= $union['u_id'];
    		$param['union_id']			= input('lid');
    		$param['agent_id']			= session('agent_id');
    		$param['store_name']		= $agent['agent_name'];
    		$param['thumb']				= $agent['logo'];
    		$param['lm_name']			= $union['name'];
    		$param['store_tel']			= $agent['mobile'];
    		$param['address']			= $agent['address'];
    		$param['customer_volume']	= input('customer_volume');
    		$param['unnion_ads_money']	= 0.2;
    		$param['add_time']			= now_time();
    		$res = Db::table('union_member')->insert($param);
			if($res){
				return json(['code'=>200,'msg'=>'成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'失败']);
			}
    		
    	}
    	$this->assign('lid',input('lid'));
    	return $this->fetch();
    }
    
    
    //退出联盟
    public function union_tuichu()
    {
    	$result = db('union_member')->where('id',input('id'))->delete();
    	if($result){
			return json(['code'=>200,'msg'=>'成功']);
		}else{
			return json(['code'=>402,'msg'=>'失败']);
		}
    }
    //联盟成员
	public function union_member()
    {
    	$list=Db::table('union_member')
    				->where('agent_id',session('agent_id'))
    				->where('union_id',input('union_id'))
    				->select();
    	$this->assign('list',$list);
    	return $this->fetch();
    }
    //联盟商圈使用帮助
	public function help1()
    {
    	return $this->fetch();
    }
    //鼓励金使用帮助
    public function help2()
    {
    	return $this->fetch();
    }
    
    
}
